package cn.itcast.web.controller.cargo;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.itcast.domain.vo.ContractProductVo;
import cn.itcast.service.cargo.ContractService;
import cn.itcast.util.DownloadUtils;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.commons.collections.map.HashedMap;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/cargo/contract", name = "出货表导出")
public class OutContractController extends BaseController {

    @Reference
    private ContractService contractService;

    @RequestMapping(value = "/print", name = "出货表下载页面")
    public String print() {
        return "cargo/print/contract-print";
    }

    @RequestMapping(value = "/printExcel", name = "excel下载")
    public void printExcel(String shipTime) throws Exception {
        // i.根据船期查询出表集合
        List<ContractProductVo> list = contractService.findContractProductVo(getCompanyId(), shipTime);
        System.out.println(list.size());

        // ii.使用easyPOI将数据封装为Workbook
        ExportParams params = new ExportParams();
        params.setTitle(shipTime.replace("-", "年") + "月份出货表");
        params.setType(ExcelType.XSSF); // 指定类型
        Workbook workbook = ExcelExportUtil.exportExcel(params, ContractProductVo.class, list);

        // iii.使用工具类完成文件下载
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        workbook.write(byteArrayOutputStream);
        DownloadUtils.download(byteArrayOutputStream, response, "出货表.xlsx");

    }

    @RequestMapping(value = "/printExcelWithTemplate", name = "excel模板下载")
    public void printExcelWithTemplate(String shipTime) throws Exception {
        // i.根据船期查询出表集合
        List<ContractProductVo> list = contractService.findContractProductVo(getCompanyId(), shipTime);
        System.out.println(list.size());

        // ii.使用easyPOI将通过模板将数据封装为Workbook
        // 1.加载模板
        String realPath = session.getServletContext().getRealPath("/make/xlsprint/poi_tempalte.xlsx");// 获取模板真实路径
        TemplateExportParams params = new TemplateExportParams(realPath);


        // 2.准备数据（模型）
        Map map = new HashedMap();
        map.put("title", shipTime.replaceAll("-", "年") + "月份出货表");
        map.put("list", list);
        // 3.封装为workbook
        Workbook workbook = ExcelExportUtil.exportExcel(params, map);

        // iii.使用工具类完成文件下载
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        workbook.write(byteArrayOutputStream);
        DownloadUtils.download(byteArrayOutputStream, response, "出货表.xlsx");

    }

    @RequestMapping(value = "/printExcelMillion", name = "excel百万数据下载")
    public void printExcelMillion(String shipTime) throws Exception {
        // i.根据船期查询出表集合
        List<ContractProductVo> list = contractService.findContractProductVo(getCompanyId(), shipTime);
        System.out.println(list.size());

        // ii.使用easyPOI将数据封装为Workbook
        ExportParams params = new ExportParams();
        params.setTitle(shipTime.replace("-", "年") + "月份出货表");
        params.setType(ExcelType.XSSF); // 指定类型

        Workbook workbook = null;
        // 循环遍历，多次添加
        for (int i = 0; i < 5000; i++) {

            workbook = ExcelExportUtil.exportBigExcel(params, ContractProductVo.class, list);
        }

        // iii.使用工具类完成文件下载
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        workbook.write(byteArrayOutputStream);
        DownloadUtils.download(byteArrayOutputStream, response, "出货表.xlsx");

    }
}
